
** Spatial approach, 4: Convert to spmatrix format ** 

	**********------------------------------**********
	* Contents: 
		* Save data structure in spatial matrix format
		* Activate exisiting matrices needed to create spatial lags 
		* Input: neighborhood sets based on spatial_3_wmat_* 
		* Works only for grid-based (step function)


capture log close 
clear all
set more off 

cd "R:\WSV2\TBu_AKe\Spatial_NEW"

capture mkdir Data
global store "R:\WSV2\TBu_AKe\Spatial_NEW\Data"


log using spatial_4_spgen_grid, replace 

	**************************
	** Load Matrices **
	**************************
	
cd "R:\WSV2\TBu_AKe\Spatial_NEW\Grid_4950"

forvalues q = 5(5)40 {
	
	spmatrix use W`q' using W`q'.stswm, replace	
	spmatrix use O`q' using W`q'.stswm, replace	

 }
 
spmatrix use H using H.stswm, replace // horizontal 
spmatrix use R using R.stswm, replace // rook (vertical)


spmatrix dir // display active
			
*********************************************
* Transform weight matrix to Finished Data **
*********************************************

cd "C:\Users\hy65byfe\Desktop\smerge_0712"
use spatial_6_regs_s_monthly, replace

drop if missing(date) // from collapse structure 

*** spset 
xtset cell kt // kt: cntry-year observation: must be balanced 
capture spset cell  // set identifier


******** Outcome: sales 
		
replace L3_sd = 0 if missing(L3_sd) // set missing to zero in pre-ban sales 
replace L3_lsd = 0 if missing(L3_lsd)  // 

		** contemp. 
replace log_sd = 0 if missing(log_sd) // 

******** Outcome: count 

replace L3_cd = 0 if missing(L3_cd) // 
replace L3_lcd = 0 if missing(L3_lcd)  //  
		
		** contemp. 
replace log_cd = 0 if missing(log_cd)


******** Load Matrices
			
spmatrix dir // display active matrices. 

******** Gen Lags 

 forvalues q = 5(5)40 {
	gen W`q'_L3_sd = 0 
	gen W`q'_L3_cd = 0 
	
	
	gen O`q'_L3_sd = 0 
	gen O`q'_L3_cd = 0 
}
 

 ***  H and R  
gen H_L3_sd = 0 
gen H_L3_cd = 0 

gen R_L3_sd = 0 
gen R_L3_cd = 0 
 

keep if year == 2014 // reduce data in memory to neccessary.  

egen k14 = group(ccode date) // country-date groups  	

drop if missing(date) // 
	
sum k14, meanonly  // date ccode group. 
scalar kmin = `r(min)' // store in scalars 
scalar kmax = `r(max)'
	
scalar list kmin kmax // 1 to 84 
 
 *** spset to verify structure 
xtset cell k14 // kt: cntry-year observation: must be balanced.
spset, clear 
spset cell  // set identifier


 ******  Create spatial variables for 1 - 8 
 
foreach var of varlist L3_sd L3_cd {
 	forvalues i = `=kmin'/`=kmax' {
	local k = `i' 
		
      spgenerate Waux_`var'_`i' = H*`var' if k14==`k'
         replace H_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
	   spgenerate Waux_`var'_`i' = R*`var' if k14==`k'
         replace R_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
      } 
} 
 

forvalues q = 5(5)40 {
 
	foreach var of varlist L3_sd L3_cd {
		
		forvalues i = `=kmin'/`=kmax' {
		local k = `i' 
		
		spgenerate Waux_`var'_`i' = W`q'*`var' if k14==`k'
         replace W`q'_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
		spgenerate Waux_`var'_`i' = O`q'*`var' if k14==`k'
         replace O`q'_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
         } 
	}
 } 
 
 
*browse cell year ccode H* R* W* O* L3_sd L3_cd if cell <= 50 & year == 2014 // 
cd "$store"
save spatial_regs_grid_monthly, replace
 
 
log close
clear
exit 